package caidan;

import java.awt.Point;

import javax.swing.SwingUtilities;

import caidan.data.FoodData;
import jinyilw.swing.ComponentTools;
import jinyilw.swing.rlaf.utils.RBorderFactory;
import jinyilw.swing.visuals.panel.Position;
import jinyilw.swing.visuals.panel.PositionSetter;

public class EditFoodPanel extends FoodComp implements PositionSetter
{
	private static final long serialVersionUID = 2643962497715625147L;
	private EditContentPanel editContentPanel;
	private Position[] choosePositions;
	private Position position, choosePosition;
	private volatile boolean choosed = false;
	private int index;

	public EditFoodPanel(FoodData fData, Position[] choosePositions, int index,
			EditContentPanel editContentPanel)
	{
		super(fData, editContentPanel.positionConfiger);
		this.choosePositions = choosePositions;
		this.index = index;
		this.editContentPanel = editContentPanel;
		RBorderFactory.setToActiveBorder(this);
		addClickOperater((Object o) ->
		{
			chooseAct();
		});
	}

	public synchronized void chooseAct()
	{
		if (getImage() == null)
			setImage(foodData.getThumbnail());

		if (!choosed)
		{
			choose();
		} else
		{
			unchoose();
		}
		// System.out.println(maxChooseNum + ":" + chooseNums[categoryIndex]);
	}

	public void choose()
	{
		choosePosition = getUnusedChoosePosition();
		if (choosePosition == null)
			return;
		choosed = true;
		editContentPanel.contentContainer.remove(this);
		editContentPanel.glassPane.add(this);
		setLocation(getConvertedPoint());
		moveToInThread(choosePosition);
		choosePosition.setUsed(true);
		choosePosition.setData(index);
		editContentPanel.glassPane.repaint();
	}

	public void unchoose()
	{
		choosed = false;
		new Thread(() ->
		{
			moveTo(getConvertedPoint());
			editContentPanel.glassPane.remove(this);
			editContentPanel.contentContainer.add(this);
			setLocation(position.getX(), position.getY());
		}).start();
		choosePosition.setUsed(false);
		editContentPanel.glassPane.repaint();
	}

	public void clear()
	{
		if (choosed)
		{
			choosed = false;
			editContentPanel.glassPane.remove(this);
			editContentPanel.contentContainer.add(this);
			setLocation(position.getX(), position.getY());
			choosePosition.setUsed(false);
		}
	}

	public void initChoose(Position p)
	{
		choosed = true;
		editContentPanel.contentContainer.remove(this);
		editContentPanel.glassPane.add(this);
		setBounds(p.getX(), p.getY(), p.getW(), p.getH());
		p.setUsed(true);
		choosePosition = p;
		choosePosition.setData(index);
	}

	public boolean isChoosed()
	{
		return choosed;
	}

	@Override
	public void setPosition(Position p)
	{
		position = p;
		if (p != null)
			setBounds(p.getX(), p.getY(), p.getW(), p.getH());
	}

	public Point getConvertedPoint()
	{
		return SwingUtilities.convertPoint(editContentPanel.contentContainer,
				position.getX(), position.getY(), editContentPanel.glassPane);
	}

	public void moveTo(Position p)
	{
		ComponentTools.moveComponent(this, p.getX(), p.getY(), 35);
	}

	public void moveToInThread(Position p)
	{
		ComponentTools.moveComponentInThread(this, p.getX(), p.getY(), 35);
	}

	public void moveTo(Point p)
	{
		ComponentTools.moveComponent(this, (int) p.getX(), (int) p.getY(), 35);
	}

	public Position getUnusedChoosePosition()
	{
		for (Position position : choosePositions)
			if (!position.isUsed())
				return position;
		return null;
	}

	public Position getPosition()
	{
		return position;
	}

	public Position getChoosePosition()
	{
		return choosePosition;
	}

	public boolean isNotMaxChoosed()
	{
		for (Position position : choosePositions)
			if (!position.isUsed())
				return true;
		return false;
	}

	public void setChoosed(boolean b)
	{
		choosed = b;
	}

	public void setIndex(int index)
	{
		this.index = index;
	}

	@Override
	public void updatePosition()
	{
		if (position != null)
		{
			position.update();
			if (!choosed)
				setBounds(position.getX(), position.getY(), position.getW(),
						position.getH());
		}
	}

//	private class ImageClickedListener extends MouseAdapter
//	{
//		@Override
//		public void mouseClicked(MouseEvent e)
//		{
//			chooseAct();
//		}
//	}
}
